package com.jay.media.music.core.notification

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.media.MediaMetadata
import android.media.session.PlaybackState
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.jay.media.kit.LogHelper
import com.jay.media.music.core.MusicService
import com.jay.media.music.core.R
import com.jay.media.music.core.db.Music
import com.jay.media.music.core.player.MusicPlayListener
import com.jay.media.music.core.player.MusicPlayManager
import com.jay.media.music.core.util.MusicExt
import com.jay.media.music.core.util.MusicExt.toMusic

/**
 * @author xuzhou4
 * @date 2022/3/17
 */
abstract class PlayingNotification {

    protected val TAG = LogHelper.makeLogTag(this::class.java)
    protected val NOTIFICATION_CHANNEL_ID = "playing_notification"
    protected val NOTIFICATION_ID = 1
    protected val NOTIFY_MODE_BACKGROUND = 0
    protected val NOTIFY_MODE_FOREGROUND = 1
    protected lateinit var mService: MusicService
    protected lateinit var mNotificationManager: NotificationManager
    protected var stopped = false
    protected var mMusic: Music? = null
    protected var mState: PlaybackState? = null

    private var mMusicPlayListener: MusicPlayListener = object : MusicPlayListener {

        override fun onMediaMetadataChange(media: MediaMetadata?) {
            LogHelper.d(TAG, "onMediaMetadataChangeChange() called with: media = $media")
            mMusic = media?.toMusic()
            update()
        }

        override fun onPlaybackStateChange(state: PlaybackState) {
            LogHelper.d(TAG, "onPlaybackStateChange() called with: state = $state")
            mState = state
            update()
        }

        override fun onMediaMetadataListChange(media: List<MediaMetadata>) {

        }
    }

    @Synchronized
    fun init(service: MusicService) {
        this.mService = service
        mNotificationManager =
            service.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
        }
        MusicPlayManager.get().registerPlayCallBack(mMusicPlayListener)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel() {
        if (mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
            val notificationChannel = NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                mService.getString(R.string.playing_notification_name),
                NotificationManager.IMPORTANCE_LOW
            )
            notificationChannel.description =
                mService.getString(R.string.playing_notification_description)
            notificationChannel.enableLights(false)
            notificationChannel.enableVibration(false)
            notificationChannel.setShowBadge(false)
            mNotificationManager.createNotificationChannel(notificationChannel)
        }
    }

    @Synchronized
    fun stop() {
        this.stopped = true
        MusicPlayManager.get().unregisterPlayCallBack(mMusicPlayListener)
        this.mService.stopForeground(true)
        this.mNotificationManager.cancel(NOTIFICATION_ID)
    }

    abstract fun update()

    /* access modifiers changed from: package-private */
    open fun updateNotifyModeAndPostNotification(notification: Notification?) {
        Log.d(
            TAG,
            "updateNotifyModeAndPostNotification() called with: notification = $notification"
        )
        this.mService.startForeground(1, notification)
    }
}